package com.cmccgz.baselibrary.image;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.cmccgz.baselibrary.R;

/**
 * @author: Huang
 * @Descripion: glide工具类
 * @date 2017/3/16 09:58
 */

public class GlideUtils {
    /**
     * 加载一张普通图片
     *
     * @param imageUrl  图片url
     * @param imageView
     */
    public static void loadImage(Activity activity, String imageUrl, final ImageView imageView) {
        GlideApp.with(activity)
                .load(imageUrl)
                .placeholder(R.drawable.n_photo_default)
                .thumbnail(0.1f)
                .into(imageView);
    }
    /**
     * 加载一张普通图片
     *
     * @param imageUrl  图片url
     * @param imageView
     */
    public static void loadImage(Context context, String imageUrl, final ImageView imageView) {
        if (context instanceof Activity) {
            loadImage(((Activity) context),imageUrl,imageView);
        }else
            GlideApp.with(context)
                    .load(imageUrl)
                    .placeholder(R.drawable.n_photo_default)
                    .thumbnail(0.1f)
                    .into(imageView);
    }
    /**
     * 加载一张普通图片
     *
     * @param imageUrl  图片url
     * @param imageView
     */
    public static void loadImage(Fragment fragment, final String imageUrl, final ImageView imageView) {
        GlideApp.with(fragment)
                .load(imageUrl)
                .placeholder(R.drawable.n_photo_default)
                .thumbnail(0.1f)
                .into(imageView);
    }
  /*  *//**
     * 加载一张普通图片
     *
     * @param context
     * @param imageUrl  图片url
     * @param imageView
     * @param listener  图片加载回调
     *//*
    public static void loadImage(Context context, String imageUrl, final ImageView imageView, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)//url
                        .placeholder(R.drawable.n_photo_default)//占位图
                        .error(R.drawable.n_photo_default)//错误图
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存机制
                        .thumbnail(0.1f)//相当于占位图，原图压缩10%
                        .listener(listener)//图片加载回调
                        .fitCenter()//缩放模式
                        .crossFade()//加载动画
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });//image，若imageView为其他圆形，方形的，第一次只会展示占位图，应用target包装
            } else {
                Glide.with(context)
                        .load(imageUrl)  .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            }
        }
    }

    *//**
     * 加载一张普通图片
     *
     * @param imageUrl  图片url
     * @param imageView
     *//*
    public static void loadImage(Fragment fragment, String imageUrl, final ImageView imageView) {
        Glide.with(fragment)
                .load(imageUrl)
                .placeholder(R.drawable.n_photo_default)
                .error(R.drawable.n_photo_default)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.1f)
                .fitCenter()
                .crossFade()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });
    }

    *//**
     * @param context
     * @param imageUri
     * @param imageView
     * @param listener
     *//*
    public static void loadImage(Context context, Uri imageUri, ImageView imageView, RequestListener<Uri, GlideDrawable> listener) {
        if (context != null && imageUri != null && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUri)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            } else {
                Glide.with(context)
                        .load(imageUri)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            }
        }
    }

    *//**
     * @param context
     * @param imageUrl
     * @param imageView
     * @param defaultImage
     * @param listener
     *//*
    public static void loadImage(Context context, String imageUrl, ImageView imageView, int defaultImage, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            }
        }
    }

    *//**
     * @param context
     * @param imageUrl
     * @param imageView
     * @param defaultImage
     * @param listener
     *//*
    public static void loadImage(Context context, String imageUrl, final CircleImageView imageView, int defaultImage, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            }
        }
    }

    *//**
     * @param context
     * @param imageUri
     * @param imageView
     * @param defaultImage
     * @param listener
     *//*
    public static void loadImage(Context context, Uri imageUri, final RoundedImageView imageView, int defaultImage, RequestListener<Uri, GlideDrawable> listener) {
        if (context != null && imageUri != null && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUri)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            } else {
                Glide.with(context)
                        .load(imageUri)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            }
        }
    }

    *//**
     * 加载一个gif图片
     *
     * @param context
     * @param gifUrl    gifUrl
     * @param imageView
     *//*
    public static void loadGif(Context context, String gifUrl, final ImageView imageView, RequestListener<String, GifDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(gifUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(gifUrl)
                        .asGif()
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .centerCrop()
                        .crossFade()
                        .into(new SimpleTarget<GifDrawable>() {
                            @Override
                            public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            } else {
                Glide.with(context)
                        .load(gifUrl)
                        .asGif()
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .centerCrop()
                        .crossFade()
                        .into(new SimpleTarget<GifDrawable>() {
                            @Override
                            public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            }
        }
    }

    *//**
     * 加载一个圆角图片
     *
     * @param context
     * @param imageUrl    图片url
     * @param imageView
     * @param roundRadius 圆角大小
     *//*
    public static void loadRoundImage(Context context, String imageUrl, final ImageView imageView, int roundRadius, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideRoundTransform(context, roundRadius))
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideRoundTransform(context, roundRadius))
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            }
        }
    }

    *//**
     * 加载一个圆形图片
     *
     * @param context
     * @param imageUrl  图片url
     * @param imageView
     *//*
    public static void loadCircleImage(Context context, String imageUrl, final ImageView imageView, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideCircleTransform(context))
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(R.drawable.n_photo_default)
                        .error(R.drawable.n_photo_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideCircleTransform(context))
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            }
        }
    }

    public static void loadCircleImage(Context context, String imageUrl, final ImageView imageView, int defaultImage, RequestListener<String, GlideDrawable> listener) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && imageView != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideCircleTransform(context))
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .transform(new GlideCircleTransform(context))
                        .fitCenter()
                        .crossFade()
                        .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                imageView.setImageDrawable(resource);
                            }
                        });
            }
        }
    }

    public static void loadHeadImage(Context context, String imageUrl, final ImageView imageView, int defaultImage, RequestListener<String, GlideDrawable> listener) {
        if (imageView == null) {
            return;
        }
        loadHeadImage(context, imageUrl, defaultImage, listener, new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                imageView.setImageDrawable(resource);
            }
        });
    }

    public static void loadHeadImage(Context context, String imageUrl, int defaultImage, RequestListener<String, GlideDrawable> listener, SimpleTarget<GlideDrawable> token) {
        if (context != null && !TextUtils.isEmpty(imageUrl) && token != null) {
            if (listener != null) {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .listener(listener)
                        .fitCenter()
                        .crossFade()
                        .into(token);
            } else {
                Glide.with(context)
                        .load(imageUrl)
                        .placeholder(defaultImage)
                        .error(defaultImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .thumbnail(0.1f)
                        .fitCenter()
                        .crossFade()
                        .into(token);
            }
        }
    }

    *//**
     * 暂停图片加载请求
     *
     * @param context
     *//*
    public static void pause(Context context) {
        Glide.with(context).pauseRequests();
    }

    *//**
     * 恢复图片加载请求
     *
     * @param context
     *//*
    public static void resume(Context context) {
        Glide.with(context).resumeRequests();
    }

    *//**
     * 清楚整个页面的图片加载请求
     *
     * @param view 页面
     *//*
    public static void clearRequest(View view) {
        Glide.clear(view);
    }

    public static void clearCache(final Context context) {
        ThreadManager.getInstance().addTask(new Runnable() {
            @Override
            public void run() {
                Glide.get(context.getApplicationContext()).clearDiskCache();
            }
        });
        Glide.get(context).clearMemory();
    }*/
}
